package com.hnucm.cooklife.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.Html;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.SPUtils;
import com.bumptech.glide.Glide;
import com.hnucm.cooklife.R;
import com.hnucm.cooklife.base.utils.Constant;
import com.hnucm.cooklife.base.utils.LoginUtils;
import com.hnucm.cooklife.bean.db.Dish;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import butterknife.BindView;
import butterknife.ButterKnife;
import de.hdodenhof.circleimageview.CircleImageView;

public class SearchResultAdapter extends RecyclerView.Adapter<SearchResultAdapter.SResultHolder> {

    private Context mContext;
    private List<Dish> mDishList = new ArrayList<>();
    private boolean isNightMode;
    private static Pattern sPattern = Pattern.compile(Constant.REGEX);

    @SuppressLint("NotifyDataSetChanged")
    public void setDishList(List<Dish> DishList){
        mDishList.clear();
        mDishList.addAll(DishList);
        notifyDataSetChanged();
    }

    public SearchResultAdapter(Context context, List<Dish> DishList) {
        mContext = context;
        mDishList.addAll(DishList);
        isNightMode = SPUtils.getInstance(Constant.CONFIG_SETTINGS)
                .getBoolean(Constant.KEY_NIGHT_MODE,false);
    }

    @NonNull
    @Override
    public SResultHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.search_result_item, parent, false);
        return new SResultHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull SResultHolder holder, int position) {
        if(mDishList != null){
            Dish Dish = mDishList.get(position);
            holder.mSearchName.setText(
                    Html.fromHtml(Dish.dishname,Html.FROM_HTML_MODE_COMPACT)
            );
            holder.mSearchAuthor.setText(
                    Html.fromHtml(Dish.username,Html.FROM_HTML_MODE_COMPACT)
            );

            String score = String.format(mContext.getResources()
                    .getString(R.string.search_score), Dish.dishscore);
            holder.mSearchScore.setText(Html.fromHtml(
                    score,Html.FROM_HTML_MODE_COMPACT
            ));

            String done = String.format(mContext.getResources()
                    .getString(R.string.search_done),Dish.dishdone);
            holder.mSearchDone.setText(Html.fromHtml(
                    done,Html.FROM_HTML_MODE_COMPACT
            ));


            Glide.with(mContext)
                    .load(Dish.dishpic)
                    .fitCenter()
                    .centerCrop()
                    .into(holder.mSearchPic);
            Glide.with(mContext)
                    .load(Dish.icon)
                    .fitCenter()
                    .centerCrop()
                    .into(holder.mSearchHeader);

            String highLightTitle = Dish.dishname;
            Matcher matcher = sPattern.matcher(highLightTitle);
            if(matcher.find()){
                highLightTitle = highLightTitle.replace("<em class='highlight'>", "").replace("</em>", "");
                setText(holder.mSearchName,highLightTitle,matcher.group(1),
                        R.color.blue);
            }

            holder.itemView.setOnClickListener(v -> {
                //todo tiaozhuan
            });

        }
    }

    private void setText(TextView tv,String text,String key,int color){
        SpannableStringBuilder builder = new SpannableStringBuilder(text);
        //第一个出现的索引位置
        int index = text.indexOf(key);
        while (index != -1){
            builder.setSpan(new ForegroundColorSpan(color),index,index + key.length(),
                    Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            //从这个索引往后开始第一个出现的位置
            index = text.indexOf(key,index + 1);
        }
        tv.setText(builder);
    }

    @Override
    public int getItemCount() {
        return mDishList != null ? mDishList.size() : 0;
    }

    class SResultHolder extends RecyclerView.ViewHolder{

        @BindView(R.id.search_pic)
        ImageView mSearchPic;

        @BindView(R.id.search_name)
        TextView mSearchName;

        @BindView(R.id.search_score)
        TextView mSearchScore;

        @BindView(R.id.search_done)
        TextView mSearchDone;

        @BindView(R.id.search_header)
        CircleImageView mSearchHeader;

        @BindView(R.id.search_author)
        TextView mSearchAuthor;

        public SResultHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }
    }
}
